<template>
  <div class="container">
    <div class="search-box">
      <el-input ref="inputRef" placeholder="请输入内容" class="input-box" v-model="inputTask"></el-input>
      <el-button @click="clearTask">清除</el-button>
      <el-button type="primary" @click="huntTask">搜索</el-button>
    </div>
    <el-table border :data="list" style="width: 100%">
      <el-table-column type="index" label="任务编号" width="80">
      </el-table-column>
      <el-table-column prop="name" label="任务名称"> </el-table-column>
      <el-table-column prop="des" label="任务描述"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template #default="{row}">
          <el-button size="mini" style="margin-right: 10px" @click="chectTast(row.id)">编辑</el-button>
          <el-popconfirm
            confirmButtonText="好的"
            cancelButtonText="不用了"
            icon="el-icon-info"
            iconColor="red"
            title="确定删除任务吗？"
            @confirm="deltask(row.id)"
          >
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pageContainer">
      <el-pagination
        layout="prev, pager, next"
        :current-page="params._page"
        :page-size="params._limit"
        :total="this.total"
        @current-change="handleCurrentChange"
      > </el-pagination>
      <el-button size="small" plain @click="addtask">+ 新增</el-button>
    </div>

    <!-- 新增的弹框 -->
    <el-dialog title="提示"  :visible.sync="dialogFormVisible" :before-close="closedia">
      <el-form :model="form" ref="form" :rules="rules">
        <el-form-item label="任务名称" prop="name" label-width="80px">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="任务描述" prop="des" label-width="80px">
          <el-input v-model="form.des" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="deldia">取 消</el-button>
        <el-button type="primary" @click="clickAddtask" >确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { tasklist, addtask, taskid, checkTaskid, delTaskid, nameTask } from '@/utils/api.js'
import { v4 as uuidv4 } from 'uuid'
export default {
  name: 'Vue',
  data () {
    return {
      // 存放任务列表数据，并双向绑定给表格
      list: [],
      // 分页的数据
      params: {
        _page: 1, // 当前第几页
        _limit: 5 // 每页条数
      },
      total: 0,
      // 控制弹框的显示隐藏
      dialogFormVisible: false,
      // 弹框表单双星绑定的数据
      form: {
        name: '',
        des: '',
        id: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入任务名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        des: [
          { required: true, message: '请输入任务描述', trigger: 'blur' },
          { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
        ]
      },
      // 双向绑定input输入框的变量
      inputTask: ''
    }
  },
  mounted () {
    // 页面启动调用请求的方法
    this.getlist()
  },
  methods: {
    // 获取用户列表的方法，并渲染
    async getlist () {
      const res = await tasklist(this.params)
      this.list = res.data
      this.total = +res.headers['x-total-count']
      // console.log(res)
    },
    // 点击分页的方法
    handleCurrentChange (newpage) {
      this.params._page = newpage
      // 调用渲染的方法
      this.getlist()
    },
    // 点击新增，弹框显示
    addtask () {
      this.dialogFormVisible = true
    },
    // 关闭的回调
    closedia (action) {
      // 清空验证
      this.$refs.form.resetFields()
      this.dialogFormVisible = false
      this.form = {
        name: '',
        des: '',
        id: ''
      }
    },
    // 弹框的确定按钮（新增 || 编辑）
    async clickAddtask () {
      if (this.form.id) {
        // 编辑的请求
        await checkTaskid(this.form)
      } else {
        // 新增的请求
        await addtask({
          ...this.form,
          id: uuidv4()
        })
      }

      // 提示用户
      this.$message.success(`${this.form.id ? '编辑成功' : '新增成功'}`)
      // 渲染
      this.getlist()
      // 关闭弹框
      // 调清空的方法
      this.closedia()
    },
    // 点击编辑
    async chectTast (id) {
      // 回显的请求
      const res = await taskid(id)
      this.form = res.data
      // 打开弹框
      this.dialogFormVisible = true
    },
    // 取消的方法
    deldia () {
      this.closedia()
    },
    // 删除的确定按钮
    async deltask (id) {
      // 删除的请求
      await delTaskid(id)
      // 渲染
      this.getlist()
      // 提示
      this.$message.success('删除成功')
    },
    // 点击搜索的方法
    async huntTask () {
      const res = await nameTask(this.inputTask)
      this.list = res.data
    },
    // 点击清除的方法
    clearTask () {
      this.inputTask = ''
      // 复原
      this.getlist()
    }
  }
}
</script>

<style scoped lang="less">
.container {
  width: 800px;
  margin: 0 auto;
}
.search-box {
  display: flex;
  margin-bottom: 10px;
  .input-box {
    margin-right: 20px;
  }
}
.pageContainer {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}
</style>
